package com.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.service.order.OrderServiceImpl;
import com.shopping.service.store.StoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderController {

    @Autowired
    OrderServiceImpl orderService;
    @Autowired
    StoreServiceImpl storeService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = {"/order/showUsersOrders"})
    public Map<String, Object> showUserOrders(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return orderService.showUsersOrders(redisUserId);
    }

    @PostMapping(value = {"/order/showStoreOrders"})
    public Map<String, Object> showStoreOrders(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        int storeId = Integer.parseInt(jsonObject.getString("storeId"));
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        if(Integer.parseInt(String.valueOf(storeService.selectStoreIdByUserId(redisUserId).get("code"))) == ResponseCode.REQUEST_SUCCESSED.getValue()){
            int selectStoreId = (int)storeService.selectStoreIdByUserId(redisUserId).get("storeId");
            if(selectStoreId == storeId){
                return orderService.showStoreOrders(storeId);
            }
        }
        map.put("code", ResponseCode.SERVER_ERROR.getValue());
        return map;
    }

    @PostMapping(value = {"/order/showOneOrder"})
    public Map<String, Object> showOneOrder(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        int id = Integer.parseInt(jsonObject.getString("id"));
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        if(Integer.parseInt(String.valueOf(orderService.selectUserIdByOrderId(id))) == ResponseCode.REQUEST_SUCCESSED.getValue()){
            int userId = Integer.parseInt(String.valueOf(orderService.selectUserIdByOrderId(id).get("userId")));
            if(userId == redisUserId){
                return orderService.showOneOrder(id);
            }
        }
        map.put("code", ResponseCode.SERVER_ERROR.getValue());
        return map;
    }
}
